/**
 * Project: eraclient
 * 
 * File Created at 2011-2-24
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.ymkyve.eraclient;

/**
 * TODO Comment of TestSysteTray
 * @author gaoliang
 *
 * @date 2011-2-24
 */
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

/**
 * swing程序最小化至系统托盘
 * 
 * @author seara
 */
public class TestSysteTray {
    
    private static Login login;
    
    public static void main(String args[]) {
        try{
            TrayIcon trayIcon = null;
            if (SystemTray.isSupported()) // 判断系统是否支持系统托盘
            {
                SystemTray tray = SystemTray.getSystemTray(); // 创建系统托盘
                Image image = Toolkit.getDefaultToolkit().getImage("/56.gif");// 载入图片,这里要写你的图标路径哦
                ActionListener listener = new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        if(login == null){
                            login = new Login();
                        }else{
                            login.setVisible(true);
                        }
                    }
                };
                // 创建弹出菜单
                PopupMenu popup = new PopupMenu();
                MenuItem defaultItem = new MenuItem("diplay");
                defaultItem.addActionListener(listener);
                MenuItem exitItem = new MenuItem("exit");
                exitItem.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        if (JOptionPane.showConfirmDialog(null, "确定退出系统") == 0) {
                            System.exit(0);
                        }
                    }
                });
                popup.add(defaultItem);
                popup.add(exitItem);
                trayIcon = new TrayIcon(image, "era", popup);// 创建trayIcon
                trayIcon.addActionListener(listener);
                try {
                    tray.add(trayIcon);
                } catch (AWTException e1) {
                    e1.printStackTrace();
                }
            }
        }catch (Exception e){
            System.out.println("出错啦");
        }
        
    }
}
